package project.parser.ast.values;

import project.environments.Value;
import project.parser.EvalException;
import project.parser.ast.aux.Record;
import project.parser.ast.exps.FuncDec;
import project.parser.ast.types.Type;

public class AbstractValue<T> implements Value {
	protected final T value;

	public AbstractValue(T value) {
		this.value = value;
	}

	@Override
	public int hashCode() {
		return value.hashCode();
	}

	@Override
	public String toString() {
		return value.toString();
	}

	@Override
	public Type type() { //override obbligatorio per la classi figlie
		return null;
	}

	@Override
	public Integer asInteger() {
		throw new EvalValueException("number", value.toString());
	}

	@Override
	public Boolean asBoolean() {
		throw new EvalValueException("boolean", value.toString());
	}

	@Override
	public Record asRecord() {
		throw new EvalValueException("record", value.toString());
	}

	@Override
	public FuncDec asFunction() {
		throw new EvalValueException("function", value.toString());
	}

	private static class EvalValueException extends EvalException {
		public EvalValueException(String expectedType, String value) {
			super("Expecting a " + expectedType + " instead of '" + value + "'");
		}
	}
}
